<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-health__articles}">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-select
            v-model="dataForm.noticeTypeId"
            clearable
            placeholder="请选择公告类型"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.title"
            placeholder="请输入公告标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t("query") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goToReleasePage()">{{
            $t("add")
          }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="deleteHandle()">{{
            $t("deleteBatch")
          }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="title"
          label="公告标题"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="noticeTypeName"
          label="公告类型"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column label="上架状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="switchChange(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="creatorName"
          label="创建者"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="updaterName"
          label="更新者"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="updateDate"
          label="更新时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="goToReleasePage(scope.row.id)"
              >{{ $t("update") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/grant/noticeList/page",
        getDataListIsPage: true,
        exportURL: "/grant/noticeList/export",
        deleteURL: "/grant/noticeList",
        deleteIsBatch: true,
      },
      dataForm: {
        id: "",
        title: "",
        noticeTypeId: ""
      },
      typeList: [],
    };
  },
  created() {
    this.listCategories();
    this.$router.afterEach((to, from) => {
      if (to.path === "/notice-noticeList" && from.path === "/notice-release") {
        this.getDataList();
      }
    });
  },
  methods: {
    goToReleasePage(id) {
      this.$router.push({
        name: "notice-release",
        params: { id: id },
      });
    },
    listCategories() {
      this.$http.get("/grant/noticeType/list").then(({ data }) => {
        this.typeList = data.data;
      });
    },
    switchChange(row) {
      this.$http
        .put("/grant/noticeList", {
          id: row.id,
          status: row.status,
        })
        .then(({ data: res }) => {
          if (res.code == 0) {
            this.$message({
              message: "上架成功",
              type: "success",
            });
          } else {
            this.$message.error("上架失败");
          }
        });
    }
  },
};
</script>